package com.dzl.order;

import com.alibaba.fastjson.JSONArray;
import com.dzl.base.controller.BaseController;
import com.dzl.order.pojo.dto.OrderBatchDeliveryDto;
import com.dzl.order.pojo.query.WelfareOrderQuery;
import com.dzl.order.pojo.vo.WelfareSupplierOrderVo;
import com.dzl.order.service.WelfareOrderService;
import com.dzl.util.JsonResult;
import com.dzl.util.PageUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 订单表(WelfareOrder)表控制层
 *
 * @author caobucao
 * @date 2021-05-21 11:36:29
 */
@RestController
@RequestMapping("/welfareOrder")
public class WelfareOrderController extends BaseController {

    @Autowired
    private WelfareOrderService service;

    /**
     * 查询各状态供应商订单数量
     * @date 2021/10/12 11:46
     * @author caobucao
     * @param query 查询条件
     * @return com.dzl.util.JsonResult<java.util.List<java.lang.Integer>>
     */
    @GetMapping("/getSupplierOrderCount")
    public JsonResult<List<Integer>> getSupplierOrderCount(WelfareOrderQuery query) {
        Integer supplierId = getLoginSupplierInfo().getId();
        query.setSupplierId(supplierId);
        return JsonResult.success(service.getSupplierOrderCount(query));
    }

    /**
     * 分页查询供应商订单列表
     * @date 2021/10/12 10:26
     * @author caobucao
     * @param query 查询条件
     * @return com.dzl.util.PageUtils<com.dzl.order.pojo.vo.WelfareSupplierOrderVo>
     */
    @GetMapping("/getSupplierOrderListPage")
    public PageUtils<WelfareSupplierOrderVo> getSupplierOrderListPage(WelfareOrderQuery query) {
        Integer supplierId = getLoginSupplierInfo().getId();
        query.setSupplierId(supplierId);
        List<WelfareSupplierOrderVo> list = service.getSupplierOrderListPage(query);
        PageInfo<WelfareSupplierOrderVo> pageInfo = new PageInfo<>(list);
        return new PageUtils<>(pageInfo);
    }

    /**
     * 获取供应商订单详情
     * @date 2021/5/28 14:43
     * @author caobucao
     * @param orderId   订单id
     * @return com.dzl.util.JsonResult<java.util.Map<java.lang.String,java.lang.Object>>
     */
    @GetMapping("getSupplierOrderDetail")
    public JsonResult<Map<String, Object>> getSupplierOrderDetail(Integer orderId) {
        Map<String, Object> map = service.selectSupplierOrderDetailByOrderId(orderId);
        return JsonResult.success(map);
    }

    /**
     * 单订单发货
     * @date 2021/5/27 17:05
     * @author caobucao
     * @param jsonArray  发货入参
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     */
    @PostMapping("orderDelivery")
    public JsonResult<Integer> orderDelivery(@RequestBody JSONArray jsonArray) {
        if (CollectionUtils.isEmpty(jsonArray)) {
            return JsonResult.paramErrWithMsg("请检查有订单编号、物流公司、物流单号/备注未填！");
        }
        List<OrderBatchDeliveryDto> dtoList = jsonArray.toJavaList(OrderBatchDeliveryDto.class);
        return JsonResult.success(service.orderDelivery(dtoList, getAdminInfo()));
    }

}
